import { businessApprovalApi } from "@/api/waybillManagement/warehouseShipment";
import { message } from "ant-design-vue";
import { Ref } from "vue";
export default function OperationHook(state: {
  drawerOpts: Base.DrawerOpts;
  passData: Ref<SingleOrderApproval.ListRows>;
  dialogOpts: Base.DialogOpts;
  formState: Ref<any>;
  emit: any;
  init: (id: string, selectNext?: boolean) => void;
  loading: Ref<boolean>;
}) {
  const { drawerOpts, dialogOpts, passData, formState, emit, init, loading } = state;
  // 查看发票
  const showInvoiceHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看发票";
    drawerOpts.type = 2;
    drawerOpts.passData = formState.value;
    drawerOpts.component = "ShowInvoice";
    drawerOpts.visible = true;
  };
  // 修改调整单价
  const changeAdjustUnitPriceHandle = () => {
    if (formState.value.freightCost.opType === "1") {
      formState.value.freightCost.clearingUnitPrice = formState.value.freightCost.unitPrice + formState.value.freightCost.adjustUnitPrice;
    } else {
      formState.value.freightCost.clearingUnitPrice = formState.value.freightCost.unitPrice - formState.value.freightCost.adjustUnitPrice;
    }
  };
  // 修改客户备注
  const modifyRemarkHandle = () => {
    dialogOpts.title = `修改客户备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = passData.value;
    dialogOpts.visible = true;
  };
  // 修改客户备注
  const modifyInnerRemarkHandle = () => {
    dialogOpts.title = `修改内部备注`;
    dialogOpts.width = "520px";
    dialogOpts.type = 2;
    dialogOpts.component = "ModifyRemark";
    dialogOpts.passData = passData.value;
    dialogOpts.visible = true;
  };
  // 查看材积明细
  const showBoxInfoHandle = () => {
    drawerOpts.width = "1200px";
    drawerOpts.title = "查看材积明细";
    drawerOpts.type = 1;
    drawerOpts.passData = passData.value;
    drawerOpts.component = "BoxInfo";
    drawerOpts.visible = true;
  };
  // 新增追加费用
  const addChargeHandle = () => {
    dialogOpts.width = "520px";
    dialogOpts.title = "追加费用";
    dialogOpts.type = 1;
    dialogOpts.passData = formState.value.bizPreOrderShipmentResDto;
    dialogOpts.component = "AddChargeFee";
    dialogOpts.visible = true;
  };
  // 通过并下一个
  const resolveHandle = () => {
    loading.value = true;
    const params: Parameters<typeof businessApprovalApi>[0] = {
      idList: [formState.value.id],
      state: 5,
      refuseType: undefined,
      refuseReason: undefined
    };
    return businessApprovalApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("审批通过成功!");
          init(formState.value.id, true);
        }
      })
      .finally(() => (loading.value = false));
  };
  // 驳回
  const rejectHandle = () => {
    dialogOpts.title = `审批驳回`;
    dialogOpts.width = "520px";
    dialogOpts.type = 1;
    dialogOpts.component = "RejectReason";
    dialogOpts.passData = formState.value;
    dialogOpts.visible = true;
  };
  // 弹窗关闭回调
  const dialogSubmitHandle = (type?: number) => {
    dialogOpts.visible = false;
    if (type) {
      // 驳回
      if (type === 1) {
        emit("reject");
      } else {
        init(formState.value.id, true);
      }
    } else {
      init(formState.value.id);
    }
  };
  return {
    showInvoiceHandle,
    changeAdjustUnitPriceHandle,
    modifyRemarkHandle,
    modifyInnerRemarkHandle,
    showBoxInfoHandle,
    addChargeHandle,
    dialogSubmitHandle,
    rejectHandle,
    resolveHandle
  };
}
